<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  H<sub>2</sub>O = 2H<sup>+</sup> + O<sup>2-</sup>
  <div id="app">
    <h1>非父子组件传值——bus，总线</h1>
    <yjf>7878234</yjf>
    <p>非父子组件传值:{{val}}</p>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    let bus = new Vue();
    Vue.component('yjf', {
      template: 
      `
        <div>
          <button @click="clickHandler">传递事件</button>
        </div>
      `,
      data() {
        return {
          info: 'yjf'
        }
      },
      methods: {
        clickHandler() {
          bus.$emit('on-msg', 'hello', this.info);
        }
      },
    })
    let vm = new Vue({
      el: '#app',
      data: function() {
        return {
          val: ''
        }
      },
      mounted() {
        // 保留this指向
        let _this = this; // 此时的this是vm
        bus.$on('on-msg', function(val1, val2) {
          console.log(val1, val2)
          // 此时的this是bus
          _this.val = val2
        })
      },
    })
  </script>
</body>
</html>